////////

#if 1
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "que_struct_type.h"
//#include "que_cpu_dis_amp.h"



#if 0
static void InitItmTypeQue(defTypeQue *q)
{
	q->front = q->rear = 0;
}
#endif
//static int getRtcMaxCount(void);
static bool que_push_back(defTypeQue *q, void* e);
//static void que_push_back(defTypeQue *q,uint16_t e);
static bool que_pop_front(defTypeQue *q, void* e);

static bool que_get_index(defTypeQue *q, uint16_t i, void* v);
static int que_length(defTypeQue *q);

#if 0
typedef struct
{
	enmTypeQue nenm;
	int nsize;
} defEnmTypeSize;

static const defEnmTypeSize m_type_size[]=
{
	{ enmTypeU08,      1 },
	{ enmTypeU16,      2 },
	{ enmTypeU32,      4 },
	{ enmTypeLaserCpu, 5 },

	{ enmTypeUnDef,    4 },
};

static int get_size_enm_que(defTypeQue *q)
{
		//defCarStatus* pCarStatus = NULL;
	int i=0;
	for(i=0;i<(int)(sizeof(m_type_size)/sizeof(defEnmTypeSize)-1);i++)
	{
		if(m_type_size[i].nenm==q->nenm)
		{
			return m_type_size[i].nsize;
			//pCarStatus = &m_nCarStatus[i];
			//return pCarStatus;
			break;
		}
	}
	return 4;
}
#endif

#define MOD_DBG_LEVEL LOG_DBG
#include "dbg_prt.h"

static void que_init(defTypeQue *q)
{
	q->front=-1;
    q->rear=-1;
	//q->front = q->rear = 0;
}
static bool que_push_back(defTypeQue *q,void* e)
{
	if( ( q->front == 0 && q->rear == q->pattr->countMax-1) || (q->front == q->rear+1) )
	{
		return false;
	}
	
	if(q->front == -1)
	{
		q->front=0;	q->rear=0;
	}
	else
	{
		if(q->rear == q->pattr->countMax-1) q->rear = 0;
		else q->rear = q->rear+1;
	}
	
	switch(q->pattr->type)
	{
		case enmTypeMcuCpuDisAmp:
			memcpy( ((defCpuDisAmp*)q->pattr->pData)+q->rear, e, sizeof(defCpuDisAmp) );
			break;
		default:
			break;
	}
	return true;
}

static bool que_pop_front(defTypeQue *q,void* e)
{
	if( q->front == -1 )
	{	return false;	}
	PRT_DBG("q->front=%d;rear=%d\n",q->front,q->rear);
	switch(q->pattr->type)
	{
		case enmTypeMcuCpuDisAmp:
			memcpy( e, ( (defCpuDisAmp*)(q->pattr->pData) )+q->front, sizeof(defCpuDisAmp) );
			defCpuDisAmp* dis_amp_pop=e;
			PRT_DBG("%02X;%02X;%02X;%02X\n"
				,dis_amp_pop->dis.u8low,dis_amp_pop->dis.unhigh.byte,dis_amp_pop->amp.u8low,dis_amp_pop->amp.u8high); 			
			break;
		default:
			break;
	}
	
	if(q->front == q->rear)
	{
		q->front = -1;
		q->rear=-1;
	}
	else
	{
		if(q->front == q->pattr->countMax-1)
			q->front = 0;
		else
			q->front = q->front+1;
	}
	PRT_DBG("q->front=%d;rear=%d\n",q->front,q->rear);
	
#if 0	
	if( q->rear == q->front )
	{
		q->rear = -1;
		q->front = 0;
	}
	else
	{
		q->front = (q->front + 1) % q->pattr->countMax;
	}
#endif		
	return true;
}

static bool que_get_index(defTypeQue *q,uint16_t index,void* v)
{
	if( q->front == -1 )
	{	return false;	}
	
	if(que_length(q) <= index) return false;
	
	index = ( q->front + index )% (q->pattr->countMax);

	switch(q->pattr->type)
	{
		case enmTypeMcuCpuDisAmp:
			//PRT_DBG("q->front=%d;index=%d\n",q->front,index);
			//memcpy( v, ((defCpuDisAmp*)q->pattr->pData) + q->front + index, sizeof(defCpuDisAmp) );
			memcpy( v, ((defCpuDisAmp*)q->pattr->pData) + index, sizeof(defCpuDisAmp) );
			break;
		default:
			break;
	}		
	
	return true;
}
static int que_length(defTypeQue *q)
{	
	if( q->front == -1 )
	{	return 0;	}
	return ( ( q->rear + q->pattr->countMax - q->front ) % q->pattr->countMax ) + 1;
#if 0	
	
	if(q->front <= q->rear)
	{	return q->rear-q->front+1; }
	else
	{	return q->rear + q->pattr->countMax - q->front + 1; }
#endif	
	//return ( ( q->rear + q->pattr->countMax - q->front ) % q->pattr->countMax ) + 1;
#if 0	
	if( q->rear >= q->front )
		return q->rear - q->front + 1;
	return q->rear + q->countMax - q->front + 1;
	//return ( ( q->rear + q->countMax - q->front ) % q->countMax ) + 1;
#endif	
}

static const defQueFunc m_que_func=
{
    //.getQueCount=getRtcMaxCount,
  .init=que_init,
  .push_back=que_push_back,
  .pop_front=que_pop_front,
  .get_index=que_get_index,
  .length=que_length,
};

const defQueFunc* get_pt_que_func(void)
{
	return &m_que_func;
}

#if 0
//const int CRtcMaxSIZE=5;
#define CRtcMaxSIZE 5
//static const  uint16_t CRtcMaxSIZE=5;
//uint16_t const CRtcMaxSIZE=5;
//static const uint16_t CRtcMaxSIZE=5;
static uint16_t RtcData[CRtcMaxSIZE];

static const defQueAttr m_rtc_attr=
{
	.type = enmTypeMcuCpuDisAmp,
	//.nsize=2, //sizeof(uint16_t)
	//.size=sizeof(RtcData)/sizeof(uint16_t)+1,
	.countMax=CRtcMaxSIZE, //+1,
	.pData=&RtcData,
};

static defTypeQue RtcQue=
{
	.pattr=(defQueAttr*)(&m_rtc_attr),
    //.count=CRtcMaxSIZE,
    .front=-1,
    .rear=-1,
};

defTypeQue* getRtcQuePt(void)
{
    return &RtcQue;
}

void prtRtcQue(void)
{
	for( int i=0; i<CRtcMaxSIZE; i++ )
	{
		
		//PRT_DBG("0x%08X\r\n", RtcData[i] );
	}
}


#endif

#endif



